import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    TouchableOpacity,
} from 'react-native';
import WFlatList from "./WFlatList";
/**
 * 对WFlatList再次封装,配合接口的数据
 */
export default class MyFlatList extends Component {

    constructor(props) {
        super(props);
        this.pageNow = 1;
        this.pageSize = 10;
        this.state = {
            messageList: ['a'],
            more:true,
        }
    }

    componentWillReceiveProps(props) {
        if (props.result) {
            // console.log('wangyu',props);
            if (props.result.messageList === this.state.messageList) {
                return;
            }
            if (this.pageNow === 1) {
                this.setState({
                    messageList: props.result.messageList,
                    more: props.result.messageList.length < props.result.total,
                });
            } else {
                let l = this.state.messageList.concat(props.result.messageList);
                this.setState({
                    messageList: l,
                    more: l.length < props.result.total
                });
            }
        }
    }

    render() {
        return (
            <WFlatList
                data={this.state.messageList}
                keyExtractor={(item, index) => this.getId()}
                onRefresh={() => {
                    this.pageNow = 1;
                    this.props.getData(this.pageNow,this.pageSize);
                }}
                more={this.state.more}
                onLoadMore={() => {
                    this.pageNow++;
                    this.props.getData(this.pageNow,this.pageSize);
                }}
                {...this.props}
            />
        );
    }

    componentDidMount() {
        this.props.getData(this.pageNow,this.pageSize);
    }

    //生成随机ID：GUID
    getId() {
        return 'xxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0,
                v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        }).toUpperCase();
    }
}

